<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/10/04 23:22:59 $)
// $RCSfile: functions_newticket.php,v $ : $Revision: 1.10 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

/**
* Sends a an email to user for new ticket
*/
function sendNewTicketEmail(&$ticketobj, &$mailObj, $tgroupid, $ticketmaskid, $email, $subject, $contents, $fromemailqueueid, $ccemaillist, $bccemaillist)
{
	global $_SWIFT, $template, $module, $dbCore;

	$template->setGroup(iif(empty($tgroupid), 1, $tgroupid));

	// ======= SET THE MESSAGE ID =======
	if (!empty($ticketobj->messageid))
	{
		$messageid = sprintf('<%s@%s>', $ticketobj->messageid, $mailObj->getLocalHost());
		$mailObj->setHeader("Message-ID", $messageid); // Set the Message-ID for this ticket
	}

	$queuesignature = $_SWIFT["queuecache"]["list"][$fromemailqueueid]["contents"];
	$staffsignature = $_SWIFT["staff"]["signature"];
	$contents = preg_replace("#(\r\n|\r|\n)#s", SWIFT_CRLF, $contents).iif(!empty($staffsignature), SWIFT_CRLF.$staffsignature, iif(!empty($queuesignature), SWIFT_CRLF.$queuesignature));

	$template->assign("contentshtml", iif($_SWIFT["settings"]["t_chtml"]==1, nl2br(htmlspecialchars($contents)), getProcessedHTML($contents)));
	$template->assign("contentstext", $contents);
	$template->assign("ishtml", true);
	$_templateresulthtml = $template->displayTemplate("email_staffnewticket", TEMPLATE_DB);
	$template->assign("ishtml", false);
	$_templateresulttext = $template->displayTemplate("email_staffnewticket", TEMPLATE_DB);

	$mailObj->setSubject(iif($_SWIFT["settings"]["t_tidinsub"]==1,$ticketobj->getEmailSubject(SUBJECT_USER),$subject));
	$mailObj->setData($_templateresulthtml, $_templateresulttext);

	$returnemail = $ticketobj->getReturnEmail();
	$returnname = $ticketobj->getReturnName();

	if (_is_array($_FILES["opt_file"]["name"]))
	{
		$fileindex = 0;
		$_filelist = array();
		foreach ($_FILES["opt_file"]["name"] as $key=>$val)
		{
			if (!empty($val))
			{
				$_filelist[$fileindex]["name"] = $val;
				$_filelist[$fileindex]["type"] = $_FILES["opt_file"]["type"][$key];
				$_filelist[$fileindex]["tmp_name"] = $_FILES["opt_file"]["tmp_name"][$key];
				$_filelist[$fileindex]["error"] = $_FILES["opt_file"]["error"][$key];
				$_filelist[$fileindex]["size"] = $_FILES["opt_file"]["size"][$key];
				$fileindex++;
			}
		}

		// We have a file to add!
		if (count($_filelist))
		{
			foreach ($_filelist as $key=>$val)
			{
				if (file_exists($val["tmp_name"]))
				{
					$mailObj->addAttachment(implode("", file($val["tmp_name"])), $val["name"], iif(!empty($val["type"]), $val["type"], 'application/octet-stream'));
				}
			}
		}
		unset($_filelist);
	}

	$_hasattachments = false;
	$downloadlink = $_POST["opt_downloads"];

	if (_is_array($downloadlink) && $module->isRegistered(MODULE_DOWNLOADS))
	{
		$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."downloaditems` WHERE `downloaditemid` IN (". buildIN($downloadlink) .");", 2);
		while ($dbCore->nextRecord(2))
		{
			if (file_exists("./files/".$dbCore->Record2["storedfilename"])) // File Exists?
			{
				// First add as mail attachment
				if (is_object($mailObj))
				{
					$mailObj->addAttachment(implode("", file("./files/".$dbCore->Record2["storedfilename"])), $dbCore->Record2["filename"], iif(!empty($dbCore->Record2["filetype"]), $dbCore->Record2["filetype"], 'application/octet-stream'));
				}
				// Then add to attachments table
			}
		}
	}


	if (count($bccemaillist))
	{
		$mailObj->sendMail($bccemaillist, $returnemail, $returnname, false);
	}

	if (count($ccemaillist))
	{
		$mailObj->setCC(implode(",", $ccemaillist));
	}
	$mailObj->sendMail(array($email), $returnemail, $returnname, false);

	return true;
}
?>